package com.czk;

import com.alibaba.fastjson.JSONObject;
import com.czk.pojo.ArticleContent;
import com.czk.pojo.Articles;
import com.czk.utlls.TokenUtil;
import com.czk.utlls.WeChatUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * 发送图文消息的测试
 *
 * @Author:ChenZhangKun
 * @Date: 2021/8/18 16:03
 */
@SpringBootTest
public class AddPicAndAirticleMessage {
    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 新增图文素材的测试
     *
     * {"media_id":"rcd4k2QJHIUxCmFrr5pUXSSyUqVLFGpqRlERy4w-WT4","item":[]}
     */
    @Test
    public void sendArticleAndPicMessage() {
        Articles articles = new Articles();
        // 创建内容
        ArticleContent articleContent = new ArticleContent();
        // 设置内容
        articleContent.setAuthor("陈彰坤");
        articleContent.setTitle("NBA最新新闻");
        // 设置mediaId
        articleContent.setThumb_media_id("rcd4k2QJHIUxCmFrr5pUXS6CZQRRmoi24JqQrypv5mw");
        articleContent.setDigest("杜兰特转会篮网");
        articleContent.setShow_cover_pic("1");
        articleContent.setContent("不知为何，当我第一次读到安东尼底薪加盟湖人，即将38岁的老甜瓜将和37岁的老詹共赴疆场的新闻，香蕉船上的兄弟如今仅有炮哥手上又新签了上亿合同，毕竟顾钱就难顾兄弟，其余人都已满足当初发下的宏愿时，我的头脑中不由得，噌，闪现出一名锅炉工的形象。\n" +
                "\n" +
                "月黑风高夜，铲煤烧火天。但见一把铁铲飞舞，突然间，刷，铲出来一本书，封面上画着詹姆斯和安东尼。");
        articleContent.setContent_source_url("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx55db64d265e84478&redirect_uri=https%3A%2F%2Ftrade.youzan.com%2Fcommon%2Fwxauth18424969&response_type=code&scope=snsapi_base&state=https%3A%2F%2Fj.youzan.com%2F9vBiU2&component_appid=wx7cd6227fafe53336#wechat_redirect");
        articleContent.setNeed_open_comment("1");
        articleContent.setOnly_fans_can_comment("0");
        //
        List<ArticleContent> list = new ArrayList<>();
        list.add(articleContent);
        articles.setArticles(list);
        String url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
        // 发送
        url = url.replace("ACCESS_TOKEN", tokenUtil.getToken().getAccessToken());
        // 转json
        String jsonString = JSONObject.toJSONString(articles);
        // 发送post请求
        String result = WeChatUtil.post(url, jsonString);
        System.out.println(result);
    }

    /**
     * 获取永久素材消息
     */
    @Test
    public void getForever() {
        String url="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
        url=url.replace("ACCESS_TOKEN",tokenUtil.getToken().getAccessToken());

        String content="{\"media_id\":\"rcd4k2QJHIUxCmFrr5pUXSSyUqVLFGpqRlERy4w-WT4\"}";
        System.out.println(WeChatUtil.post(url, content));
    }
}
